package com.hospital.demo.controller.lxy;



import com.hospital.demo.mapper.lxy.DutyMapper;
import com.hospital.demo.mapper.lxy.WorkersMapper;
import com.hospital.demo.model.lxy.Duty;
import com.hospital.demo.model.lxy.Workers;
import com.hospital.demo.model.lxy.Workstation;
import com.hospital.demo.service.lxy.WorkstationService;
import com.hospital.demo.util.ResultUtils;
import com.hospital.demo.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李欣远
 * @since 2020-04-08
 */
@RestController
@RequestMapping("/hospital/workstation")
public class WorkstationController {
    @Autowired
    private WorkstationService workstationService;
    @Autowired
    private WorkersMapper workersMapper;
    @Autowired
    private DutyMapper dutyMapper;
    @Autowired
    JavaMailSenderImpl mailSender;
    @RequestMapping("/wss")
    public ResultVO list(Model model){
        List<Workstation> wss= workstationService.selWs();
        model.addAttribute("wss",wss);
        return ResultUtils.success(wss);
    }
    @GetMapping("/searchWs")
    public ResultVO searchWs(Integer did) {
        Workstation workstation = new Workstation();
        workstation.setWsid(did);

        List<Workstation> list = workstationService.selWsByD(did);
        return ResultUtils.success(list);
    }
//    @RequestMapping("/wss/{did}")
//    public List<Workstation> listD(@PathVariable("did")Integer did, Model model){
//        List<Workstation> wss= workstationService.selWsByD(did);
//        model.addAttribute("wss",wss);
//        return wss;
//    }

//    @GetMapping("/ws")
//    public String toAddpage(Model model){
//        List<Duty> dutys=dutyMapper.selectList(null);
//        model.addAttribute("dutys",dutys);
//        return "http://localhost:8080/#/page/ws/operation/add_cur.html";
//    }

    @PostMapping("/ws")
    public ResultVO addWs(Workstation workstation){
//        workstationService.save(workstation);
        Duty duty = dutyMapper.selByWsdtid(workstation.getWsdtid());
        Workers workers = workersMapper.selectById(duty.getDtdid());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        String dmail=workers.getDmail();
        mailMessage.setSubject("新增值班计划");
        mailMessage.setText(workers.getDname()+"医生您好,您新增加的值班计划为"+workstation.getWsid()+"号，请注意查看");

        mailMessage.setTo(dmail);
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
        return ResultUtils.success(workstationService.save(workstation));
    }
//    @GetMapping("/updws")
//    public String toUpdateEmp(@RequestParam(name = "wsid", defaultValue = "-1")Integer wsid, Model model){
//        Workstation workstationById=workstationService.getById(wsid);
//        model.addAttribute("workstationById",workstationById);
//        QueryWrapper<Workers> wrapper2 = new QueryWrapper<>();
//        wrapper2.select("dname");
//        List<Workers> workers=workersMapper.selectList(wrapper2);
//        model.addAttribute("workers",workers);
//        return "ws/operation/update_cur.html";
//    }
    @PostMapping("/updateWs")
    public ResultVO updateWs(Workstation workstation){
//        workstationService.updateById(workstation);
        Duty duty = dutyMapper.selByWsdtid(workstation.getWsdtid());
        Workers workers = workersMapper.selectById(duty.getDtdid());
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        String dmail=workers.getDmail();
        mailMessage.setSubject("值班计划变动");
        mailMessage.setText(workers.getDname()+"医生您好,您的"+workstation.getWsid()+"号值班计划已变动，请注意查看");

        mailMessage.setTo(dmail);
        mailMessage.setFrom("3597945105@qq.com");
        mailSender.send(mailMessage);
        return ResultUtils.success(workstationService.updateById(workstation));
    }
    @PostMapping("/delWs")
    public ResultVO deleteWs(@RequestParam(name = "wsid", defaultValue = "-1")Integer wsid){

        return ResultUtils.success(workstationService.removeById(wsid));
    }
}

